


// 防火墙视图
void Request_View_Firewall(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 加载防火墙页面
		LoadPage(c, 200, HTTP_CT_HTML, "firewall.html");
		
	} else {
		
		// 其他请求方法返回 404 页面
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
		
	}
}



#ifdef _WIN32
// Windows系统不支持防火墙功能
static void route_http_firewall_list(struct mg_connection *c, struct mg_http_message *hm) {
    http_reply(c, 400, HTTP_CT_JSON, "防火墙功能仅支持Linux系统", 0);
}

static void route_http_firewall_save(struct mg_connection *c, struct mg_http_message *hm) {
    http_reply(c, 400, HTTP_CT_JSON, "防火墙功能仅支持Linux系统", 0);
}

static void route_http_firewall_add(struct mg_connection *c, struct mg_http_message *hm) {
    http_reply(c, 400, HTTP_CT_JSON, "防火墙功能仅支持Linux系统", 0);
}

static void route_http_firewall_delete(struct mg_connection *c, struct mg_http_message *hm) {
    http_reply(c, 400, HTTP_CT_JSON, "防火墙功能仅支持Linux系统", 0);
}

static void route_http_firewall_toggle(struct mg_connection *c, struct mg_http_message *hm) {
    http_reply(c, 400, HTTP_CT_JSON, "防火墙功能仅支持Linux系统", 0);
}

#else



// 获取防火墙规则列表
static void route_http_firewall_list(struct mg_connection *c, struct mg_http_message *hm) {
    
    // 从iptables读取当前规则
    char* rules_json = firewall_get_rules();
    if (!rules_json) {
        http_reply(c, 500, HTTP_CT_JSON, "获取防火墙规则失败", 0);
        return;
    }
    
    // 构建响应
    char* response = (char*)malloc(strlen(rules_json) + 256);
    sprintf(response, "{\"code\":0,\"msg\":\"success\",\"data\":%s}", rules_json);
    
    http_reply(c, 200, HTTP_CT_JSON, response, 0);
    
    free(rules_json);
    free(response);
}

// 保存防火墙规则配置
static void route_http_firewall_save(struct mg_connection *c, struct mg_http_message *hm) {
    
    // 获取POST数据
    struct mg_str rules = mg_http_var(hm->body, mg_str("rules"));
    if (rules.len == 0) {
        http_reply(c, 400, HTTP_CT_JSON, "缺少规则数据", 0);
        return;
    }
    
    // 保存配置到文件
    if (firewall_save_config(rules) != 0) {
        http_reply(c, 500, HTTP_CT_JSON, "保存配置失败", 0);
        return;
    }
    
    // 应用规则到iptables
    char* rules_str = (char*)malloc(rules.len + 1);
    memcpy(rules_str, rules.buf, rules.len);
    rules_str[rules.len] = '\0';
    
    if (firewall_apply_all_rules(rules_str) != 0) {
        free(rules_str);
        http_reply(c, 500, HTTP_CT_JSON, "应用防火墙规则失败", 0);
        return;
    }
    
    free(rules_str);
    
    http_reply(c, 200, HTTP_CT_JSON, "保存成功", 0);
}

// 添加防火墙规则
static void route_http_firewall_add(struct mg_connection *c, struct mg_http_message *hm) {
    
    // 获取参数
    char protocol[32], port[64], source_ip[128], action[32], comment[256];
    
    mg_http_get_var(&hm->body, "protocol", protocol, sizeof(protocol));
    mg_http_get_var(&hm->body, "port", port, sizeof(port));
    mg_http_get_var(&hm->body, "source_ip", source_ip, sizeof(source_ip));
    mg_http_get_var(&hm->body, "action", action, sizeof(action));
    mg_http_get_var(&hm->body, "comment", comment, sizeof(comment));
    
    if (strlen(protocol) == 0 || strlen(action) == 0) {
        http_reply(c, 400, HTTP_CT_JSON, "缺少必要参数", 0);
        return;
    }
    
    // 生成规则ID（使用时间戳）
    int rule_id = (int)time(NULL);
    
    // 构建规则
    FirewallRule rule;
    memset(&rule, 0, sizeof(rule));
    rule.id = rule_id;
    strncpy(rule.protocol, protocol, sizeof(rule.protocol) - 1);
    strncpy(rule.port, port, sizeof(rule.port) - 1);
    strncpy(rule.source_ip, source_ip, sizeof(rule.source_ip) - 1);
    strncpy(rule.action, action, sizeof(rule.action) - 1);
    strncpy(rule.comment, comment, sizeof(rule.comment) - 1);
    rule.enabled = 1;
    
    // 应用规则
    if (firewall_apply_rule(&rule) != 0) {
        http_reply(c, 500, HTTP_CT_JSON, "添加防火墙规则失败", 0);
        return;
    }
    
    // 读取当前所有规则
    char* rules_json = firewall_get_rules();
    if (rules_json) {
        // 保存到配置文件
        firewall_save_config(mg_str(rules_json));
        free(rules_json);
    }
    
    http_reply(c, 200, HTTP_CT_JSON, "添加成功", 0);
}

// 删除防火墙规则
static void route_http_firewall_delete(struct mg_connection *c, struct mg_http_message *hm) {
    
    // 获取规则ID
    char id_str[32];
    mg_http_get_var(&hm->body, "id", id_str, sizeof(id_str));
    
    if (strlen(id_str) == 0) {
        http_reply(c, 400, HTTP_CT_JSON, "缺少规则ID", 0);
        return;
    }
    
    int rule_id = atoi(id_str);
    
    // 删除指定规则
    char cmd[512];
    char* output = exec_cmd("iptables -L INPUT --line-numbers -n | grep 'xpanel_fw_' | awk '{print $1, $0}'");
    
    if (output) {
        char* line = strtok(output, "\n");
        while (line) {
            char comment_search[64];
            snprintf(comment_search, sizeof(comment_search), "xpanel_fw_%d", rule_id);
            
            if (strstr(line, comment_search)) {
                int line_num;
                sscanf(line, "%d", &line_num);
                snprintf(cmd, sizeof(cmd), "iptables -D INPUT %d", line_num);
                system(cmd);
                printf("删除防火墙规则 #%d\n", rule_id);
                break;
            }
            
            line = strtok(NULL, "\n");
        }
        free(output);
    }
    
    // 更新配置文件
    char* rules_json = firewall_get_rules();
    if (rules_json) {
        firewall_save_config(mg_str(rules_json));
        free(rules_json);
    }
    
    // 保存iptables
    system("iptables-save > /etc/iptables/rules.v4 2>/dev/null || true");
    
    http_reply(c, 200, HTTP_CT_JSON, "删除成功", 0);
}

// 启用/禁用防火墙规则
static void route_http_firewall_toggle(struct mg_connection *c, struct mg_http_message *hm) {
    
    // 获取参数
    char id_str[32], enabled_str[8];
    mg_http_get_var(&hm->body, "id", id_str, sizeof(id_str));
    mg_http_get_var(&hm->body, "enabled", enabled_str, sizeof(enabled_str));
    
    if (strlen(id_str) == 0) {
        http_reply(c, 400, HTTP_CT_JSON, "缺少规则ID", 0);
        return;
    }
    
    int rule_id = atoi(id_str);
    int enabled = atoi(enabled_str);
    
    // 重新加载配置并应用
    char* config = firewall_load_config();
    if (config) {
        firewall_apply_all_rules(config);
        free(config);
    }
    
    http_reply(c, 200, HTTP_CT_JSON, enabled ? "已启用" : "已禁用", 0);
}

#endif // _WIN32
